package com.zyk.scaffold.common.utils;

import org.apache.commons.lang3.RandomStringUtils;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.UUID;

/**
 * 随机数工具类
 */
public class RandomUtils {
    /***
     * 生成随机数
     * @param count
     * @return java.lang.String
     **/
    public static String randomNumeric(int count) {
        return RandomStringUtils.randomNumeric(count);
    }

    /***
     * 生成uuid
     * @param
     * @return java.lang.String
     **/
    public static String uuid() {
        String uuid = UUID.randomUUID().toString();
        return uuid.replace("-", "");
    }
    /**
     *  随机生成指定位数的字母数字组合
     *  @Param  [length]
     *  @Return      java.lang.String
     *  @Exception
     */
    public static String getCharAndNumr(int length) {
        String val = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            // 输出字母还是数字
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            // 字符串
            if ("char".equalsIgnoreCase(charOrNum)) {
                // 取得大写字母还是小写字母
                int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char) (choice + random.nextInt(26));
                // 数字
            } else {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }

    public static Set<String> buildBathSting(String target, int num){
        HashSet<String> strings = new HashSet<>();
        for(int i = 0; i < num; i++){
            Random random = new Random();
            char[] chars = new char[target.length()];
            int finalI = i;
            int[] ints = target.chars().map(charrr -> {
                if(charrr>=48 && charrr<=57){
                    return 48 + random.nextInt(9);
                }
                if(charrr>=65 && charrr<=90){
                    return 65 + random.nextInt(25);
                }
                if(charrr>=97 && charrr<=122){
                    return 97 + random.nextInt(25);
                }
                if(charrr<=127){
                    return charrr;
                }
                int bb = random.nextInt(2) % 2 == 0 ? charrr + finalI : charrr - finalI;
                return bb < 0 ? random.nextInt(charrr) : bb;
            }).toArray();
            for(int j = 0; j<ints.length; j++){
                chars[j] = (char) ints[j];
            }
            String result = new String(chars);
            strings.add(result);
        }
        return strings;
    }
    public static void main(String[] args){
        buildBathSting("陕A:98634H", 10).forEach(
                s -> System.out.println(s)
        );
    }
}
